<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script type="text/javascript" src="js/jquery-2.1.0.js"></script>
</head>
<script>
    $(function () {

        $("#logout").click(function () {
            $.ajax("/user/logout",{
                type:"get",
                data:{},
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);
                    window.location.href="login.html";
                },
                error:function(){
                }
            })
        });

        $("#search1").click(function () {
            var bookName = $("input[name = 'name']").val();
            $.ajax("/user/search",{
                type:"get",
                data:{
                    'bookName':bookName
                },
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);


                    $("#page").hide();
                    var dataObj = eval('('+data+')');
                    var div = document.getElementById('table');
                    $('.tr1').remove();
                    for (var i = 0;i < dataObj.length;i++){
                        var newDiv = document.createElement('tr');
                        div.appendChild(newDiv);
                        newDiv.setAttribute('class', 'tr1');
                        newDiv.innerHTML = "<td>"+dataObj[i].name+"</td>"+
                                           "<td>"+dataObj[i].number+"</td>"+
                                           "<td>"+dataObj[i].type+"</td>"+
                                           "<td>"+dataObj[i].publishedtime+"</td>"+
                                           "<td id='borrow' class='addmark11' style='cursor: pointer'>借书</td>";

                        var detail = document.getElementsByClassName('addmark11');

                        detail[i].index = i;
                        detail[i].onclick = function(){
                            var count = this.index;
                            var id = parseInt(dataObj[count].id);
                            borrowBook(id);
                        };
                    }
                },
                error:function(){

                }
            });

        });


        $("#filter").click(function () {
            var type = $("input[name='radio1']:checked").val();
            $.ajax("/user/filter",{
                type:"get",
                data:{
                    'type':type
                },
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);


                    $("#page").hide();
                    var dataObj = eval('('+data+')');
                    var div = document.getElementById('table');
                    $('.tr1').remove();
                    for (var i = 0;i < dataObj.length;i++){
                        var newDiv = document.createElement('tr');
                        div.appendChild(newDiv);
                        newDiv.setAttribute('class', 'tr1');
                        newDiv.innerHTML = "<td>"+dataObj[i].name+"</td>"+
                            "<td>"+dataObj[i].number+"</td>"+
                            "<td>"+dataObj[i].type+"</td>"+
                            "<td>"+dataObj[i].publishedtime+"</td>"+
                            "<td id='borrow' class='addmark12' style='cursor: pointer'>借书</td>";

                        var detail = document.getElementsByClassName('addmark12');

                        detail[i].index = i;
                        detail[i].onclick = function(){
                            var count = this.index;
                            var id = parseInt(dataObj[count].id);
                            borrowBook(id);
                        };
                    }
                },
                error:function(){

                }
            });

        });

        $("#filter").click(function () {
            var type = $("input[name='radio1']:checked").val();
            $.ajax("/user/filter",{
                type:"get",
                data:{
                    'type':type
                },
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);


                    $("#page").hide();
                    var dataObj = eval('('+data+')');
                    var div = document.getElementById('table');
                    $('.tr1').remove();
                    for (var i = 0;i < dataObj.length;i++){
                        var newDiv = document.createElement('tr');
                        div.appendChild(newDiv);
                        newDiv.setAttribute('class', 'tr1');
                        newDiv.innerHTML = "<td>"+dataObj[i].name+"</td>"+
                            "<td>"+dataObj[i].number+"</td>"+
                            "<td>"+dataObj[i].type+"</td>"+
                            "<td>"+dataObj[i].publishedtime+"</td>"+
                            "<td id='borrow' class='addmark13' style='cursor: pointer'>借书</td>";

                        var detail = document.getElementsByClassName('addmark13');

                        detail[i].index = i;
                        detail[i].onclick = function(){
                            var count = this.index;
                            var id = parseInt(dataObj[count].id);
                            borrowBook(id);
                        };

                    }
                },
                error:function(){

                }
            });

        });

        $.ajax("/user/pageSize",{
            type:"get",
            data:{},
            success:function(data){
//                      在这里注册成功后跳转页面
                console.log(data);

                var dataObj = eval('('+data+')');
                var pageSize = parseInt(dataObj);
                var pages = parseInt(pageSize/10)+1;
                $("#totalPages").text(pages);
                var td = document.getElementById('td1');
                for (var i = 1;i <= pages;i++){
                    var newDiv = document.createElement('span');
                    td.appendChild(newDiv);
                    newDiv.innerHTML = "<span style='cursor: pointer' class='addmark'>"+i+"</span>&nbsp;&nbsp;";

                    var detail = document.getElementsByClassName('addmark');

                    detail[i-1].index = i-1;
                    detail[i-1].onclick = function(){
                        var count = this.index;
                        index1(count);
                    };

                }
            },
            error:function(){

            }
        });

        function index1(count) {

            $.ajax("/user/allBooks",{
                type:"get",
                data:{
                    'Offset':count
                },
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);

                    var dataObj = eval('('+data+')');
                    var div = document.getElementById('table');
                    $('.tr1').remove();
                    for (var i = 0;i < dataObj.length;i++){
                        var newDiv = document.createElement('tr');
                        div.appendChild(newDiv);
                        newDiv.setAttribute('class', 'tr1');
                        newDiv.innerHTML = "<td>"+dataObj[i].name+"</td>"+
                            "<td>"+dataObj[i].number+"</td>"+
                            "<td>"+dataObj[i].type+"</td>"+
                            "<td>"+dataObj[i].publishedtime+"</td>"+
                            "<td id='borrow' class='addmark14' style='cursor: pointer'>借书</td>";

                        var detail = document.getElementsByClassName('addmark14');

                        detail[i].index = i;
                        detail[i].onclick = function(){
                            var count = this.index;
                            var id = parseInt(dataObj[count].id);
                            borrowBook(id);
                        };
                    }
                },
                error:function(){

                }
            });
        }


        $.ajax("/user/allBooks",{
            type:"get",
            data:{
                'Offset':0
            },
            success:function(data){
//                      在这里注册成功后跳转页面
                console.log(data);

                var dataObj = eval('('+data+')');
                var div = document.getElementById('table');
                $('.tr1').remove();
                for (var i = 0;i < dataObj.length;i++){
                    var newDiv = document.createElement('tr');
                    div.appendChild(newDiv);
                    newDiv.setAttribute('class', 'tr1');
                    newDiv.innerHTML = "<td>"+dataObj[i].name+"</td>"+
                        "<td>"+dataObj[i].number+"</td>"+
                        "<td>"+dataObj[i].type+"</td>"+
                        "<td>"+dataObj[i].publishedtime+"</td>"+
                        "<td id='borrow' class='addmark15' style='cursor: pointer'>借书</td>";

                    var detail = document.getElementsByClassName('addmark15');

                    detail[i].index = i;
                    detail[i].onclick = function(){
                        var count = this.index;
                        var id = parseInt(dataObj[count].id);
                        borrowBook(id);
                    };

                }
            },
            error:function(){

            }
        });

        function borrowBook(id) {

            $.ajax("/user/borrow",{
                type:"get",
                data:{
                    'id':id
                },
                success:function(data){
//                      在这里注册成功后跳转页面
                    console.log(data);
                    if (data == "true"){
                        alert("借书成功");
                    }else {
                        alert("借书失败");
                    }
                },
                error:function(){

                }
            });
        }

    })
</script>
<body>
<div style="position: absolute;right:10px;margin-right: 40px;top: 10px;">
    <a style="cursor: pointer;text-decoration: none" href="person.html">个人中心</a>&nbsp;&nbsp;<span id="logout" style="cursor: pointer">登出</span>
</div>
<hr style="margin-top: 40px"/>
<div style="position: absolute;left: 400px;text-align: center">
    <h1>图书列表</h1>
    <table border="1" id="table">
        <tr>
            <td colspan="5"><input type="text" id="search" name="name" placeholder="书名搜索"/>&nbsp;&nbsp;<button id="search1">搜索</button></td>
        </tr>
        <tr>
            <td colspan="5">
                <span>类型：</span>
                <input type="radio" name="radio1" value="文学" />文学
                <input type="radio" name="radio1" value="教育考试" />教育考试
                <input type="radio" name="radio1" value="经管" />经管
                <input type="radio" name="radio1" value="科技" />科技
                <input type="radio" name="radio1" value="人文社科" />人文社科
                &nbsp;&nbsp;<button id="filter">检索</button>
            </td>
        </tr>
        <tr>
            <th>名字</th>
            <th>数量</th>
            <th>类型</th>
            <th>出版时间</th>
            <th>操作</th>
        </tr>
        <tr id="page">
            <td colspan="5" id="td1">共<span id="totalPages"></span>页&nbsp;&nbsp;</td>
        </tr>
    </table>
</div>
</body>
</html>